



import 'package:erp/EventBus/EventBus.dart';
import 'package:test/test.dart';


void main(){

  test("EventBus", (){

    var app_onceTicker = 0;
    var app_everyTimeTicker = 0;

    var example_onceTicker = 0;
    var example_everyTimeTicker = 0;

    var bus = EventBus.singleton();

    bus.subscribeOnce<MySubject>("app", (bus, data){
      expect(data.name, "yegan0");
      app_onceTicker++;
    });

    bus.subscribeOnce<MySubject>("example", (bus, data){
      expect(data.name, "yegan0");
      example_onceTicker++;
    });

    bus.subscribe<MySubject>("app", (bus, data){
      expect(data.description, "person");
      app_everyTimeTicker++;

      data.description = "description changed!";
    });

    bus.subscribe<MySubject>("example", (bus, data){
      expect(data.description, "person");
      example_everyTimeTicker++;
    });

    var app_person = bus.publish("app", MySubject(
        name: "yegan0",
        description: "person"
    ));
    expect(app_person.description, "description changed!");
    bus.publish("app", MySubject(
        name: "yegan0",
        description: "person"
    ));

    bus.subscribe<MySubject>("error", (bus, data){
      expect(false, true);
    });

    bus.publish("example", MySubject(
        name: "yegan0",
        description: "person"
    ));
    bus.publish("example", MySubject(
        name: "yegan0",
        description: "person"
    ));

    expect(app_onceTicker, 1);
    expect(app_everyTimeTicker, 2);

    expect(example_onceTicker, 1);
    expect(example_everyTimeTicker, 2);

    EventBus.singleton()
    ..unSubscribeAll("app.example")
    ..publish("app.example", MySubject(
        name: "yegan0",
        description: "person"
    ));

    expect(app_onceTicker, 1);
    expect(app_everyTimeTicker, 2);


  });

  test("like", (){
    var counter1 = 0;
    var counter2 = 0;
    var counter3 = 0;
    var counter4 = 0;
    var counter5 = 0;
    var counter6 = 0;
    var counter7 = 0;
    var counter8 = 0;
    var counter9 = 0;
    var counter10 = 0;

    var bus = EventBus.singleton(key: "like");

    bus.subscribe<String>("0.0", (bus, data){
      counter1++;
    });
    bus.subscribe<String>("0.1", (bus, data){
      counter2++;
    });
    bus.subscribe<String>("1.0", (bus, data){
      counter3++;
    });
    bus.subscribe<String>("1.1", (bus, data){
      counter4++;
    });

    bus.subscribe<String>("1.*", (bus, data){
      counter5++;
    });
    bus.subscribe<String>("0.*", (bus, data){
      counter6++;
    });
    bus.subscribe<String>("*.1", (bus, data){
      counter7++;
    });
    bus.subscribe<String>("*.0", (bus, data){
      counter8++;
    });
    bus.subscribe<String>("*.*", (bus, data){
      counter9++;
    });
    bus.subscribe<String>("%", (bus, data){
      counter10++;
    });

    bus.publish("0.0", "");
    expect(counter1, 1); //0.0
    expect(counter2, 0); //0.1
    expect(counter3, 0); //1.0
    expect(counter4, 0); //1.1
    expect(counter5, 0); //1.*
    expect(counter6, 1); //0.*
    expect(counter7, 0); //*.1
    expect(counter8, 1); //*.0
    expect(counter9, 1); //*.*
    expect(counter10, 1);//%

    bus.publish("0.1", "");
    expect(counter1, 1); //0.0
    expect(counter2, 1); //0.1
    expect(counter3, 0); //1.0
    expect(counter4, 0); //1.1
    expect(counter5, 0); //1.*
    expect(counter6, 2); //0.*
    expect(counter7, 1); //*.1
    expect(counter8, 1); //*.0
    expect(counter9, 2); //*.*
    expect(counter10, 2);//%

    bus.publish("1.1", "");
    expect(counter1, 1); //0.0
    expect(counter2, 1); //0.1
    expect(counter3, 0); //1.0
    expect(counter4, 1); //1.1
    expect(counter5, 1); //1.*
    expect(counter6, 2); //0.*
    expect(counter7, 2); //*.1
    expect(counter8, 1); //*.0
    expect(counter9, 3); //*.*
    expect(counter10, 3);//%

    bus.publish("%", "");
    expect(counter1, 2); //0.0
    expect(counter2, 2); //0.1
    expect(counter3, 1); //1.0
    expect(counter4, 2); //1.1
    expect(counter5, 2); //1.*
    expect(counter6, 3); //0.*
    expect(counter7, 3); //*.1
    expect(counter8, 2); //*.0
    expect(counter9, 4); //*.*
    expect(counter10, 4);//%

    bus.publish("*.*", "");
    expect(counter1, 3); //0.0
    expect(counter2, 3); //0.1
    expect(counter3, 2); //1.0
    expect(counter4, 3); //1.1
    expect(counter5, 3); //1.*
    expect(counter6, 4); //0.*
    expect(counter7, 4); //*.1
    expect(counter8, 3); //*.0
    expect(counter9, 5); //*.*
    expect(counter10, 5);//%

    bus.publish("*.1", "");
    expect(counter1, 3); //0.0
    expect(counter2, 4); //0.1
    expect(counter3, 2); //1.0
    expect(counter4, 4); //1.1
    expect(counter5, 4); //1.*
    expect(counter6, 5); //0.*
    expect(counter7, 5); //*.1
    expect(counter8, 3); //*.0
    expect(counter9, 6); //*.*
    expect(counter10, 6);//%

    bus.publish("1.*", "");
    expect(counter1, 3); //0.0
    expect(counter2, 4); //0.1
    expect(counter3, 3); //1.0
    expect(counter4, 5); //1.1
    expect(counter5, 5); //1.*
    expect(counter6, 5); //0.*
    expect(counter7, 6); //*.1
    expect(counter8, 4); //*.0
    expect(counter9, 7); //*.*
    expect(counter10, 7);//%
  });
}

//object.operation.state

//keyword.search.error
//keyword.search.success
//keyword.update.success
//network.download.error
//

class MySubject {
  String name;
  String description;
  MySubject({this.name, this.description});
}

